package map.coreui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.zjw.zy.R
import com.zjw.zy.base.BaseRecyclerAdapter
import com.zjw.zy.coreui.view.SearchView
import com.zjw.zy.ext.alertConfirm
import com.zjw.zy.ext.go
import com.zjw.zy.ext.longToast
import com.zjw.zy.utils.PhoneUtils
import map.coreui.fragment.adapter.SuggestSearchAdapter
import map.coreui.fragment.holder.SuggestSearchHolder
import map.coreui.view.BackView
import map.coreui.view.PoiDetailView
import map.ext.mapView
import map.viewmodel.PreviewViewModel
import map.viewmodel.RoutePlanViewModel
import map.viewmodel.SearchViewModel
import map.viewmodel.SuggestSearchViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/25 10:22
 * @description：
 */
class SuggestSearchFragment : MapBaseFragment() {

    private val searchViewModel: SearchViewModel by viewModels()
    private val suggestViewModel: SuggestSearchViewModel by viewModels()
    private val previewViewModel: PreviewViewModel by viewModels()

    private val routeViewModel: RoutePlanViewModel by activityViewModels()

    private val mSearchBar: SearchView by lazy { findViewById(R.id.search_fragment_search) }
    private val mList: RecyclerView by lazy { findViewById(R.id.search_fragment_list) }
    private val poiDetails: PoiDetailView by lazy { findViewById(R.id.poi_detail) }
    private val mBack: BackView by lazy { findViewById(R.id.suggest_search_back) }
    private val mAdapter: SuggestSearchAdapter by lazy {
        SuggestSearchAdapter(object : BaseRecyclerAdapter.ItemListener<SuggestSearchHolder> {
            override fun onItemClick(holder: SuggestSearchHolder, v: View) {
                mList.visibility = View.GONE
                searchViewModel.addSuggestRecord(holder.data)
                if (holder.data.suggestionInfo.uid.isEmpty()) {
                    searchViewModel.nearSearch(holder.data.suggestionInfo.key)
                } else {
                    searchViewModel.poiSearch(holder.data.suggestionInfo.uid)
                }
            }
        })
    }

    override fun onPageEnter() {
        suggestViewModel.onPageEnter()
        poiDetails.visibility = View.GONE
        mapView().showZoomControls(false)
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        searchViewModel.getSuggestResult().observe(this) {
            if (it.isEmpty()) {
                mList.visibility = View.GONE
            } else {
                mList.visibility = View.VISIBLE
            }
            mAdapter.notifyData(it)
        }

        searchViewModel.getPoiDetails().observe(this) {
            it?.let {
                previewViewModel.previewDetailWithMe(it)
            }
            onPoiDetails(it)
        }

        suggestViewModel.getPoiDetails().observe(this) {
            onPoiDetails(it)
        }

    }

    private fun onPoiDetails(it: PoiDetailResult?) {
        it?.let {
            poiDetails.visibility = View.VISIBLE
            poiDetails.bindData(it)
        } ?: run {
            poiDetails.visibility = View.GONE
        }
    }

    override fun onBindViewListener() {
        mSearchBar.bindListener(object : SearchView.OnSearchListener {
            override fun onSearchTextChange(keyword: String) {
                searchViewModel.suggestSearch(keyword)
            }
        })
        poiDetails.bindListener(object : PoiDetailView.PoiDetailListener {
            override fun onClickGo(poi: PoiDetailResult) {
                routeViewModel.walkPlan(poi.location) {
                    if (it) {
                        //规划成功，跳转规划页面
                        go(R.id.nav_map_multiroute_fragment)
                    } else {
                        "路线规划失败".longToast()
                    }
                }
            }

            override fun onClickDetails(detailUrl: String) {
                //详情网页
            }

            override fun onClickPhone(text: CharSequence?) {
                text?.let {
                    alertConfirm(title = "联系商家？", activity = requireActivity()) {
                        if (it) {
                            PhoneUtils.call(it.toString())
                        }
                    }
                }
            }
        })
    }

    override fun onBindViewValue() {
        mList.adapter = mAdapter
        mList.layoutManager = LinearLayoutManager(context)

        mBack.attachTo(this)
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.search_fragment
    }

    override fun onPageExit() {
        suggestViewModel.onPageExit()
    }
}